package com.dd.keel.core.framework.proxy;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Proxy connection factory
 * @author Kevin.XU
 *
 */
public class ProxyConnFactory extends BasePooledObjectFactory<ProxyConn>{
	
	final static private Logger LOGGER = LoggerFactory.getLogger(ProxyConnFactory.class);
	
	private String addr = null;
	
	private String host = null;
	private int port = 0;

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
		String[] array = this.addr.split(":");
		if(array.length == 2){
			this.host = array[0];
			this.port = Integer.parseInt(array[1]);
		}
	}

	@Override
	public ProxyConn create() throws Exception {
		ProxyConn conn = new ProxyConn(host,port);
		if(!conn.connect()){
			throw new Exception("Connect proxy server failed : "+host+", "+port);
		}
		return conn;
	}

	@Override
	public PooledObject<ProxyConn> wrap(ProxyConn arg0) {
		return new DefaultPooledObject<ProxyConn>(arg0);
	}
	
}
